package com.example.triviahangman;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {
	
	Button btnA; 
	TextView txtAnswer;
	String strCheck;
	String strMasked ;
	StringBuffer strRevealed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main);
        
        btnA = (Button) findViewById(R.id.buttonA);
        txtAnswer = (TextView) findViewById(R.id.txtAns);
        strCheck = (String) txtAnswer.getText();
        
        char positions[] = new char[strCheck.length()];
        for (int i=0; i<strCheck.length(); i++) {
        	positions[i] = '-';
        }
        strMasked = new String(positions);
        strRevealed = new StringBuffer(strMasked);
        txtAnswer.setText(strMasked);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    public void slctA(View view) {
    	
    	/* msgAns = "Button -A- Pressed";
    	txtAnswer.setText(msgAns); */
    	
    	if (strCheck.indexOf('a') == -1) {
    		Context context = getApplicationContext();
    		CharSequence text = "That letter is not present.";
    		int duration = Toast.LENGTH_LONG;
    	
    		Toast toast = Toast.makeText(context, text, duration);
    		toast.show();  
    	} else {
    		/*Context context = getApplicationContext();
    		CharSequence text = "The letter -A- is present.";
    		int duration = Toast.LENGTH_LONG;
    	
    		Toast toast = Toast.makeText(context, text, duration);
    		toast.show(); */
    		
    		for (int i=0; i<strMasked.length(); i++) {
    			if (strCheck.charAt(i) == 'a') {
    				strRevealed.setCharAt(i, 'a');
    			}
    		}
    		txtAnswer.setText(strRevealed);
    	}
    }
    
public void slctB(View view) {
    	
    	/* msgAns = "Button -A- Pressed";
    	txtAnswer.setText(msgAns); */
    	
    	if (strCheck.indexOf('b') == -1) {
    		Context context = getApplicationContext();
    		CharSequence text = "That letter is not present.";
    		int duration = Toast.LENGTH_LONG;
    	
    		Toast toast = Toast.makeText(context, text, duration);
    		toast.show();  
    	} else {
    		/*Context context = getApplicationContext();
    		CharSequence text = "The letter -A- is present.";
    		int duration = Toast.LENGTH_LONG;
    	
    		Toast toast = Toast.makeText(context, text, duration);
    		toast.show(); */
    		
    		for (int i=0; i<strMasked.length(); i++) {
    			if (strCheck.charAt(i) == 'b') {
    				strRevealed.setCharAt(i, 'b');
    			}
    		}
    		txtAnswer.setText(strRevealed);
    	}
    }
}
